package com.Blog.www.dao.Impl;


import com.Blog.www.dao.CommentDao;
import com.Blog.www.po.BlogComment;
import com.Blog.www.utils.SqlDao;

import java.util.List;

/**
 * 博客评论相关的dao操作
 *
 * @author 君未洋
 */
public class CommentDaoImpl implements CommentDao {
    private static SqlDao dao = new SqlDao();

    @Override
    public boolean addComment(String bid, String uid, String comment) {
        String sql = "INSERT INTO blogcomment VALUES (null,?,?,0,?)";
        return dao.update(sql, uid, bid, comment);
    }

    @Override
    public List<BlogComment> getCommentList(int id, int prior) {
        String sql = "select * from blogcomment where blogid=? and prior=?";
        return dao.selectAllComment(sql, id, prior);
    }

    @Override
    public boolean delete(String cid) {
        String sql = "delete from blogcomment where id =?";
        return dao.update(sql, cid);
    }

    @Override
    public BlogComment getComment(int id) {
        String sql = "select * from blogcomment where id =?";
        List<BlogComment> blogComments = dao.selectAllComment(sql, id);
        if (blogComments == null || blogComments.size() == 0) {
            return null;
        } else {
            return blogComments.get(0);
        }
    }

    @Override
    public boolean addReply(String bid, String uid, String cid, String comment) {
        String sql = "INSERT INTO blogcomment VALUES (null,?,?,?,?)";
        return dao.update(sql, uid, bid, cid, comment);
    }

    @Override
    public int findCommentTotalCount(String id) {
        String sql = "select count(*) as count from blogcomment where 1=1";
        if (id != null) {
            sql = sql + " blogid = ? ";
            return dao.selectCount(sql, id);
        }
        return dao.selectCount(sql);
    }

    @Override
    public List<BlogComment> findCommentList(int start, int rows, String bid) {
        if (bid == null) {
            String sql = "select * from blogcomment limit ?,?";
            return dao.selectAllComment(sql, start, rows);
        }
        String sql = "select * from blogcomment where blogid = ? limit ?,?";
        return dao.selectAllComment(bid, start, rows);

    }
}
